package com.hzncc.flowable_diboot.controller.flowable;

import com.diboot.core.vo.JsonResult;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author GaoGuoPei
 * @describe
 * @date 2024/3/21
 **/
@RestController
@RequestMapping("/flowable/task")
public class TaskController {
    private final TaskService taskService;

    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    /**
     * 根据分组查询任务
     * @param group
     * @return
     * @throws Exception
     */
    @PostMapping("/queryTaskByGroup")
    public JsonResult queryTaskByGroup(@RequestParam String group) throws Exception {
        List<Task> list = taskService.createTaskQuery().taskCandidateGroup(group).list();
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        for (Task task : list) {
            // 根据任务id查询当前任务参数
            Map<String, Object> variables = taskService.getVariables(task.getId());
            variables.put("taskId", task.getId());
            result.add(variables);
        }
        return JsonResult.OK(result);
    }

    /**
     * 按照分配组查询，可以指定多个组 比如一个人有两个职位，就应该看到两个分组的task
     * @param candidateGroups 组list
     * @return
     * @throws Exception
     */
    @PostMapping("/queryTaskByCandidateGroupIn")
    public JsonResult queryTaskByCandidateGroupIn(@RequestBody List<String> candidateGroups) throws Exception {
        List<Task> list = taskService.createTaskQuery().taskCandidateGroupIn(candidateGroups).list();
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        for (Task task : list) {
            // 根据任务id查询当前任务参数
            Map<String, Object> variables = taskService.getVariables(task.getId());
            variables.put("taskId", task.getId());
            result.add(variables);
        }
        return JsonResult.OK(result);
    }

    /**
     * 按照指派人查询，直接查询表 ACT_RU_TASK
     * @param assignee  指派人 指userId
     * @return
     * @throws Exception
     */
    @PostMapping("/queryTaskByAssignee")
    public JsonResult queryTaskByAssignee(@RequestParam String assignee) throws Exception {
        List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        for (Task task : list) {
            // 根据任务id查询当前任务参数
            Map<String, Object> variables = taskService.getVariables(task.getId());
            variables.put("taskId", task.getId());
            result.add(variables);
        }
        return JsonResult.OK(result);
    }

    /**
     * 按照分配组 OR 指派人查询，用到了 ACT_ID_*表的数据，传入userId查询其所在权限组。
     * 首先查询用户所在组权限
     * 然后查询指派人任务或者指派人为空并且用户所在权限组的任务
     * @param assignee  指派人 指userId
     * @return
     * @throws Exception
     */
    @PostMapping("/queryTaskByCandidateOrAssigned")
    public JsonResult queryTaskByCandidateOrAssigned(@RequestParam String assignee) throws Exception {
        List<Task> list = taskService.createTaskQuery().taskCandidateOrAssigned(assignee).list();
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        for (Task task : list) {
            // 根据任务id查询当前任务参数
            Map<String, Object> variables = taskService.getVariables(task.getId());
            variables.put("taskId", task.getId());
            result.add(variables);
        }
        return JsonResult.OK(result);
    }

    /**
     * 先查找用户所属的组（用户所属用户组的信息从 Activiti 的 ACT_ID_*表获取）
     * 如果找到了用户组信息
     * 如果找不到用户所属的组
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/queryTaskByCandidateUser")
    public JsonResult queryTaskByCandidateUser(@RequestParam String userId) throws Exception {
        List<Task> list = taskService.createTaskQuery().taskCandidateUser(userId).list();
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        for (Task task : list) {
            // 根据任务id查询当前任务参数
            Map<String, Object> variables = taskService.getVariables(task.getId());
            variables.put("taskId", task.getId());
            result.add(variables);
        }
        return JsonResult.OK(result);
    }

    /**
     * 按照所属人查询
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/queryTaskByOwner")
    public JsonResult queryTaskByOwner(@RequestParam String userId) throws Exception {
        List<Task> list = taskService.createTaskQuery().taskOwner(userId).list();
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        for (Task task : list) {
            // 根据任务id查询当前任务参数
            Map<String, Object> variables = taskService.getVariables(task.getId());
            variables.put("taskId", task.getId());
            result.add(variables);
        }
        return JsonResult.OK(result);
    }


    /**
     * 审批任务
     * @param approval
     * @return
     * @throws Exception
     */
    @PostMapping("/approvalTask")
    public JsonResult approvalTask(@RequestBody Map<String, Object> approval) throws Exception {
        String taskId = approval.get("taskId").toString();
        approval.remove("taskId");
        taskService.complete(taskId, approval);
        return JsonResult.OK();
    }

}
